﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ERP_RORZE_TOOL.Common;
using ERP_RORZE_TOOL.Common.ValueObject;

namespace ERP_RORZE_TOOL.DBAccess
{
	class SupplierDAO : BaseDAO
	{
		#region Basic Method

		public static DataTable GetAll()
		{
			return ExcuteProcedure(ToolConstants.StoreProcedure.Supplier.GetAll);
		}

		public static DataTable GetOne(string supplierId)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
						CreateParameter("@SupplierID", SqlDbType.NVarChar, supplierId)
                    };
				return ExcuteProcedure(ToolConstants.StoreProcedure.Supplier.GetOne, parameters);

			}
			catch(Exception e)
			{
				throw new MyException(e);
			}

		}

		public static Supplier LoadOne(string supplierId)
		{
			Supplier o = new Supplier();
			DataTable dt = GetOne(supplierId);
			if(dt.Rows.Count > 0)
			{
				o.SupplierID = dt.Rows[0][ToolConstants.Entities.Supplier.FieldName.SupplierID].ToString();
				o.SupplierName = dt.Rows[0][ToolConstants.Entities.Supplier.FieldName.SupplierName].ToString();
                //o.Company = dt.Rows[0][ToolConstants.Entities.Supplier.FieldName.Company].ToString();
                //o.Address = dt.Rows[0][ToolConstants.Entities.Supplier.FieldName.Address].ToString();
                //o.CountryID = int.Parse(dt.Rows[0][ToolConstants.Entities.Supplier.FieldName.CountryID].ToString());
                //o.Telephone = dt.Rows[0][ToolConstants.Entities.Supplier.FieldName.Telephone].ToString();
                //o.Fax = dt.Rows[0][ToolConstants.Entities.Supplier.FieldName.Fax].ToString();
                //o.PIC = dt.Rows[0][ToolConstants.Entities.Supplier.FieldName.PIC].ToString();
                //o.PICSex = dt.Rows[0][ToolConstants.Entities.Supplier.FieldName.PICSex].ToString();
                //o.PICEmail = dt.Rows[0][ToolConstants.Entities.Supplier.FieldName.PICEmail].ToString();
                //o.PICPosition = dt.Rows[0][ToolConstants.Entities.Supplier.FieldName.PICPosition].ToString();
                //o.TaxCode = dt.Rows[0][ToolConstants.Entities.Supplier.FieldName.TaxCode].ToString();
                //o.BankAccount = dt.Rows[0][ToolConstants.Entities.Supplier.FieldName.BankAccount].ToString();
                //o.BankName = dt.Rows[0][ToolConstants.Entities.Supplier.FieldName.BankName].ToString();
                //o.NameOfCommodity = dt.Rows[0][ToolConstants.Entities.Supplier.FieldName.NameOfCommodity].ToString();
                //o.DeliveryTermtID = int.Parse(dt.Rows[0][ToolConstants.Entities.Supplier.FieldName.DeliveryTermtID].ToString());
                //o.PaymentID = int.Parse(dt.Rows[0][ToolConstants.Entities.Supplier.FieldName.PaymentID].ToString());
                //o.Note = dt.Rows[0][ToolConstants.Entities.Supplier.FieldName.Note].ToString();

			}

			return o;
		}


		public static int Insert(Supplier o)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
                       
                        CreateParameter("@SupplierID", SqlDbType.NVarChar, o.SupplierID),
                        CreateParameter("@SupplierName", SqlDbType.NVarChar, o.SupplierName),
                        //CreateParameter("@Company", SqlDbType.NVarChar, o.Company),
                        //CreateParameter("@Address", SqlDbType.NVarChar, o.Address),
                        //CreateParameter("@CountryID", SqlDbType.Int, o.CountryID),
                        //CreateParameter("@Telephone", SqlDbType.NVarChar, o.Telephone),
                        //CreateParameter("@Fax", SqlDbType.NVarChar, o.Fax),
                        //CreateParameter("@PIC", SqlDbType.NVarChar, o.PIC),
                        //CreateParameter("@PICSex", SqlDbType.NVarChar, o.PICSex),
                        //CreateParameter("@PICEmail", SqlDbType.NVarChar, o.PICEmail),
                        //CreateParameter("@PICPosition", SqlDbType.NVarChar, o.PICPosition),
                        //CreateParameter("@TaxCode", SqlDbType.NVarChar, o.TaxCode),
                        //CreateParameter("@BankAccount", SqlDbType.NVarChar, o.BankAccount),
                        //CreateParameter("@BankName", SqlDbType.NVarChar, o.BankName),
                        //CreateParameter("@Commodity", SqlDbType.NVarChar, o.NameOfCommodity),
                        //CreateParameter("@DeliveryPointID", SqlDbType.Int, o.DeliveryTermtID),
                        //CreateParameter("@PaymentID", SqlDbType.Int, o.PaymentID),
                        //CreateParameter("@Note", SqlDbType.NVarChar, o.Note),
                    };

				return ExecuteNonQueryProcedure(ToolConstants.StoreProcedure.Supplier.Insert, parameters);

			}
			catch(Exception e)
			{
				throw new MyException(e);
			}

		}

		public static int Update(Supplier o)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
						 CreateParameter("@TempID", SqlDbType.NVarChar, o.TempId),
                        CreateParameter("@SupplierID", SqlDbType.NVarChar, o.SupplierID),
                        CreateParameter("@SupplierName", SqlDbType.NVarChar, o.SupplierName),
                        //CreateParameter("@Company", SqlDbType.NVarChar, o.Company),
                        //CreateParameter("@Address", SqlDbType.NVarChar, o.Address),
                        //CreateParameter("@CountryID", SqlDbType.Int, o.CountryID),
                        //CreateParameter("@Telephone", SqlDbType.NVarChar, o.Telephone),
                        //CreateParameter("@Fax", SqlDbType.NVarChar, o.Fax),
                        //CreateParameter("@PIC", SqlDbType.NVarChar, o.PIC),
                        //CreateParameter("@PICSex", SqlDbType.NVarChar, o.PICSex),
                        //CreateParameter("@PICEmail", SqlDbType.NVarChar, o.PICEmail),
                        //CreateParameter("@PICPosition", SqlDbType.NVarChar, o.PICPosition),
                        //CreateParameter("@TaxCode", SqlDbType.NVarChar, o.TaxCode),
                        //CreateParameter("@BankAccount", SqlDbType.NVarChar, o.BankAccount),
                        //CreateParameter("@BankName", SqlDbType.NVarChar, o.BankName),
                        //CreateParameter("@Commodity", SqlDbType.NVarChar, o.NameOfCommodity),
                        //CreateParameter("@DeliveryPointID", SqlDbType.Int, o.DeliveryTermtID),
                        //CreateParameter("@PaymentID", SqlDbType.Int, o.PaymentID),
                        //CreateParameter("@Note", SqlDbType.NVarChar, o.Note),
                    };

				return ExecuteNonQueryProcedure(ToolConstants.StoreProcedure.Supplier.Update, parameters);
			}
			catch(Exception e)
			{
				throw new MyException(e);
			}
		}

		public static int Delete(Supplier o)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
						CreateParameter("@SupplierID", SqlDbType.NVarChar, o.SupplierID),
                    };

				return ExecuteNonQueryProcedure(ToolConstants.StoreProcedure.Supplier.Delete, parameters);
			}
			catch(Exception e)
			{
				throw new MyException(e);
			}
		}

		public static DataTable GetTemplateTable()
		{
			DataTable table = new DataTable("222_Supplier");

			return table;
		}

		#endregion
	}
}
